<?php
class CarritoController 
	extends Zend_Controller_Action 
{
	
	public function miCarritoAction() {
		
		$mapper	= Delivery_Model_Factory::mapper( 'Negocios' );
		
		$negocios = $this->carrito->getNegocios();
		
		$mapper->loadHorarios( $negocios );
		
	}
	
	public function agregarAction() {
		$idProducto = (int) $this->_getParam('idProducto');
		$cantidad 	= (int) $this->_getParam('cantidad');
		$opciones 	= trim($this->_getParam('opciones'), "-");
		
		try{
		
			$result = $this->carrito->agregar( $idProducto, $cantidad, $opciones);
			
			$result['success'] = true;
		
			$this->_helper->Json($result);	
			
		} catch (Delivery_Model_Exception $e){
			
			$result = array(
				'success' => false,
				'message'=>	$this->_helper->Translate( $e->getMessage() )		
			);			
			$this->getHelper('Log')->exception( $e );
			$this->_helper->Json($result);			
		}catch( Zend_Exception $e){
		
			$response = array('message' => $this->_helper->Translate("applicationError"), 'success' => false);
			
			$this->getHelper('Log')->exception( $e );
			$this->_helper->Json($result);		
		}
			
	}
	
	public function agregarPromocionAction() {
		$idPromocion= (int) $this->_getParam('idPromocion');
		$cantidad 	= (int) $this->_getParam('cantidad');
		$productos 	= trim($this->_getParam('productos'), "-");
		
		try{
		
			$result = $this->carrito->agregarPromocion( $idPromocion, $cantidad, $productos);
			
			$result['success'] = true;
		
			$this->_helper->Json($result);	
			
			
		} catch (Delivery_Model_Exception $e){
			
			$result = array(
				'success' => false,
				'message'=>	$this->_helper->Translate($e->getMessage())		
			);			
			$this->getHelper('Log')->exception( $e );
			$this->_helper->Json($result);	
					
		}catch( Zend_Exception $e){
		
			$result = array('message' => $this->_helper->Translate("applicationError"), 'success' => false);
			$this->getHelper('Log')->exception( $e );
			$this->_helper->Json( $result );			
		}
			
	}
	
	public function vaciarAction() {
		
		$idNegocio = $this->_getParam('idNegocio');
		
		$this->carrito->vaciar($idNegocio);
		
		if( $this->_request->isXmlHttpRequest()){
			
			$result = array(
				'success' => true
			);			
			$this->_helper->Json($result);	
			
		} else {
			return 
				$this->getHelper('Redirector')->gotoRoute(array(), 'my-cart');		
		}
	}
	
	public function removeAction() {
		
		try{
		
			$productoKey = $this->_getParam('productoKey');
			$promoKey = $this->_getParam('promoKey');
			
			if ( $productoKey ){
				$result = $this->carrito->eliminarProducto( $productoKey );
			}
			
			if ( $promoKey ){
				$result = $this->carrito->eliminarPromocion( $promoKey );
			}
			
			$result['success'] = true;
		
			$this->_helper->Json($result);	
			
			
		} catch (Delivery_Model_Exception $e){
			
			$result = array(
				'success' => false,
				'message'=>	$this->_helper->Translate($e->getMessage())		
			);			
			$this->getHelper('Log')->exception( $e );
			$this->_helper->Json($result);	
					
		}catch( Zend_Exception $e){
		
			$result = array('message' => $this->_helper->Translate("applicationError"), 'success' => false);
			$this->getHelper('Log')->exception( $e );
			$this->_helper->Json( $result );			
		}
	
	}
	
	public function updateAction() {
		
		try{
		
			$productoKey = $this->_getParam('productoKey');
			$promoKey = $this->_getParam('promoKey');
			$cantidad = $this->_getParam('cantidad');
			
			if ( $productoKey ){
				$result = $this->carrito->actualizarProducto( $productoKey, $cantidad );
			}
			
			if ( $promoKey ){
				$result = $this->carrito->actualizarPromocion( $promoKey, $cantidad );
			}
			
			$result['success'] = true;
		
			$this->_helper->Json($result);	
			
			
		} catch (Delivery_Model_Exception $e){
			
			$result = array(
				'success' => false,
				'message'=>	$this->_helper->Translate($e->getMessage())		
			);			
			$this->getHelper('Log')->exception( $e );
			$this->_helper->Json($result);	
					
		}catch( Zend_Exception $e){
		
			$result = array('message' => $this->_helper->Translate("applicationError"), 'success' => false);
			$this->getHelper('Log')->exception( $e );
			$this->_helper->Json( $result );			
		}
	
	}
	
	public function sideAction()
	{
		$this->_helper->layout->disableLayout();
	}
	
	public function opcionAction()
	{
	    $session 	= Zend_Registry::get('session');
	    $name    = $this->_getParam('name');
	    $value    = $this->_getParam('value');
	
	    $session->$name = $value;
	    
	    $this->_helper->Json( true );
	    
	}

}




